package com.luck.emp.whorder.web;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;

import com.luck.emp.entity.EmpTThM;
import com.luck.emp.entity.EmpTWhOrderD;
import com.luck.emp.entity.EmpTWhOrderM;
import com.luck.emp.whorder.logic.IWhorderLogic;
import com.luck.emp.whorder.qry.WhorderQry;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.ObjectUtil;
import com.luck.pf.util.Page;

/**
 * 说明:ACTION类
 * Copyright: Copyright (c) 2011
 * 深圳**科技
 * @author 
 * 
 */
public class WhorderAction extends BaseActionSupport {
		/**
		 * 逻辑处理类
		 */
		IWhorderLogic whorderLogic;
		/**
		 * 查询对象
		 */
		WhorderQry qry;
		/**
		 * 分页对象
		 **/
		Page page;
		/**
		 * 实体类
		 */
		EmpTWhOrderM entity;
		List<EmpTWhOrderD> orderds;
		String outIn;//out:出库,in:入库
		String[] productCodes;//商品数组
		String [] prices;//实际采购价
		String[] qtys;//数量数组
		/**
		 * 入库单
		 */
		public String inList() throws Exception{
			UserBean userInfo = super.getUserInfo();
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (WhorderQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new WhorderQry();
			}
			if(qry.getStartDate() == null)
				qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(DateUtil.getDate(), -30)));
			if(qry.getEndDate() == null)
				qry.setEndDate(DateUtil.getEndDateDay(DateUtil.getDate()));
			qry.setIoKind("2");
			qry.setUserInfo(userInfo);
			page = whorderLogic.findWhorderPage(qry, Page.class);
			return "list";
		}

		/**
		 * 出库单
		 */
		public String outList() throws Exception{
			UserBean userInfo = super.getUserInfo();
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (WhorderQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new WhorderQry();
			}
			if(qry.getStartDate() == null)
				qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(DateUtil.getDate(), -30)));
			if(qry.getEndDate() == null)
				qry.setEndDate(DateUtil.getEndDateDay(DateUtil.getDate()));
			qry.setIoKind("1");
			qry.setUserInfo(userInfo);
			page = whorderLogic.findWhorderPage(qry, Page.class);
			return "list";
		}
		
		/**
		 * 查看
		 **/
		public String view() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = whorderLogic.get(EmpTWhOrderM.class,entity.getOrderCode());
			orderds = whorderLogic.findWhOrderD(entity.getOrderCode());
			super.title="出入库管理详细信息";
			return "view";
		}
		public String print() throws Exception{
			entity = whorderLogic.get(EmpTWhOrderM.class,entity.getOrderCode());
			orderds = whorderLogic.findWhOrderD(entity.getOrderCode());
			super.title="出入库详细信息";
			return "print";
		}
		public String toAdd() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = new EmpTWhOrderM();
			super.event="ADD";
			if(outIn.equals("out")){
				entity.setIoKind("1");
				super.title = "新增出库单";
			}else{
				entity.setIoKind("2");
				super.title = "新增入库单";
			}
			return "edit";
		}
		public String edit() throws Exception{
			UserBean userInfo = super.getUserInfo();
			String str = "";
			//新增操作
			try {
				entity.setWhCode(null);
				str = whorderLogic.saveWhorder(userInfo, entity,productCodes, prices, qtys);
			} catch (Exception e) {
				str = e.getMessage();
			}
			if(StringUtils.isNotBlank(str)){
				super.msgStr = str;
				return super.GLOBAL_ERROR;
			}
			if(entity.getIoKind().equals("1"))
				super.redirectUrl = "/emp/whorder_outList.action";
			else
				super.redirectUrl = "/emp/whorder_inList.action";
			return super.GLOBAL_SUCCESS;
		}
		public String auditList() throws Exception{
			UserBean userInfo = super.getUserInfo();
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (WhorderQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new WhorderQry();
			}
			if(qry.getStartDate() == null)
				qry.setStartDate(DateUtil.getStartDateDay(DateUtils.addDays(DateUtil.getDate(), -30)));
			if(qry.getEndDate() == null)
				qry.setEndDate(DateUtil.getEndDateDay(DateUtil.getDate()));
			qry.setUserInfo(userInfo);
			page = whorderLogic.findWhorderPage(qry, Page.class);
			return "auditList";
		}
		public String toAudit() throws Exception{
			entity = whorderLogic.get(EmpTWhOrderM.class,entity.getOrderCode());
			orderds = whorderLogic.findWhOrderD(entity.getOrderCode());
			super.title="审核出入库单";
			return "audit";
		}
		public String audit() throws Exception{
			UserBean userBean = super.getUserInfo();
			entity = whorderLogic.get(EmpTWhOrderM.class,entity.getOrderCode());
			whorderLogic.saveAudit(userBean, entity, qry);
			super.redirectUrl = "/emp/whorder_auditList.action";
			return super.GLOBAL_SUCCESS;
		}
		public WhorderQry getQry(){
			return this.qry;
		}
		public void setQry(WhorderQry qry){
			this.qry = qry;
		}
		
		public IWhorderLogic getWhorderLogic(){
			return this.whorderLogic;
		}
		public void setWhorderLogic(IWhorderLogic whorderLogic){
			this.whorderLogic = whorderLogic;
		}
		public Page getPage() {
			return this.page;
		}
		public void setPage(Page page) {
			this.page = page;
		}
		public EmpTWhOrderM getEntity(){
			return entity;
		}
		public void setEntity(EmpTWhOrderM entity){
			this.entity = entity;
		}

		public List<EmpTWhOrderD> getOrderds() {
			return orderds;
		}

		public void setOrderds(List<EmpTWhOrderD> orderds) {
			this.orderds = orderds;
		}

		public String getOutIn() {
			return outIn;
		}

		public void setOutIn(String outIn) {
			this.outIn = outIn;
		}

		public String[] getProductCodes() {
			return productCodes;
		}

		public void setProductCodes(String[] productCodes) {
			this.productCodes = productCodes;
		}

		public String[] getPrices() {
			return prices;
		}

		public void setPrices(String[] prices) {
			this.prices = prices;
		}

		public String[] getQtys() {
			return qtys;
		}

		public void setQtys(String[] qtys) {
			this.qtys = qtys;
		}
		
}